﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using Htss.Platform.Web;

namespace DreamBox.Web.Box
{
    public class searchBox:ControllerBox
    {
        public ActionResult Load()
        {
            if (Request.QueryString["kw"] != null && Request.QueryString["kw"] != "")
            {
                string kw =Htss.Platform.Core.Text.Replace(Request.QueryString["kw"]);

                //
                int p = Request.QueryString["p"] != null ? Htss.Platform.Core.Transform.GetInt(Request.QueryString["p"], 1) : 1;

                string url = "/search?kw=" + kw;

                string where = "";
                //分页数据处理
                DAL.GetPager page = new DAL.GetPager();
                where = "IsStatus=1 and Title like '%" + kw + "%' ";
                ViewData["ListData"] = GetPageDt(page.GetFormPageData(p, "dbox_v_posts", "PostsID", where, 1), kw);
                ViewData["ListPage"] = page.GetPage(p, where, "dbox_v_posts", "PostsID", url)[0];

            }
            return View("search");
        }
        public DataTable GetPageDt(DataTable Dt, string Kw)
        {
            //实例化新DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Title", typeof(string)));
            dt.Columns.Add(new DataColumn("Contents", typeof(string)));
            dt.Columns.Add(new DataColumn("Url", typeof(string)));
            dt.Columns.Add(new DataColumn("Time", typeof(string)));
            dt.Columns.Add(new DataColumn("Total", typeof(int)));
            dt.Columns.Add(new DataColumn("UserID", typeof(string)));
            dt.Columns.Add(new DataColumn("UserName", typeof(string)));
            dt.Columns.Add(new DataColumn("Avatar", typeof(string)));
            foreach (DataRow row in Dt.Rows)
            {
                //处理地址
                string url = "/read/" + row["PostsID"];
                string title = row["Title"].ToString();

                //数据填充
                DataRow NewRow = dt.NewRow();
                NewRow["Title"] = title.Replace(Kw, "<font color='red'>" + Kw + "</font>");

                NewRow["Contents"] = "";//row["Contents"].ToString();

                NewRow["Url"] = url;
                NewRow["Time"] = DateTime.Parse(row["PostDate"].ToString()).ToShortDateString();

                NewRow["UserID"] = row["UserID"];
                NewRow["UserName"] = row["UserName"];
                NewRow["Avatar"] = row["Avatar"];

                //处理统计列
                Regex r = new Regex(@"(" + Kw + ")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                NewRow["Total"] = r.Matches(title).Count;

                //填充行
                dt.Rows.Add(NewRow);
            }
            dt.DefaultView.Sort = "Total desc";

            return dt;
        }
    }
}
